<template>
	<view class="page">
		<!-- #ifndef MP-WEIXIN-->
		<my-navbar leftText="识别结果" @leftClick="back"></my-navbar>
		<!-- #endif -->
		<view class="goods_box">
			<view class="identify">
				<span class="result-txt">识别结果：</span>
				<view class="list-box">
					<span class="lf-m-10 green-txt" @click="succhage(1)">成功{{dataList.result.success}}个</span>
					<span class="lf-m-10 red-txt" @click="succhage(2)">失败{{dataList.result.fails }}个</span>
					<span class="lf-m-10 gray-txt" @click="succhage(3)">模糊识别{{dataList.result.likes}}个</span>
				</view>
			</view>

			<view class="food-box">
				<view class="shops-box" v-for="(item,index) in tablist">
					<view class="pics">
						<image class="picture" :src="item.img" mode="widthFix"></image>
					</view>
					<view class="flex1 lf-m-l15">
						<view>
							<view class="red">￥{{item.order_price}}/{{item.unit}}</view>
							<view class="name-color">{{item.goods_name}}</view>
						</view>
						<view class="inventory">
							<text v-if="item.minimum>0">起订量：{{item.minimum}}</text>
						</view>
					</view>

					<view class="add-box">
						<image class="add" @click="addgoods(1,index,item)" src="/static/image/sub.svg"></image>
						<view class="add-txt" v-show="!item.showinput" @click="addnum(item)">{{item.order_num}}
						</view>
						<input v-show="item.showinput" class="border" type="digit" placeholder=""
							@input="onKeyInput($event,item)" @blur="onKeyInputBlur(item)" v-model="item.order_num" />
						<image class="add" @click="addgoods(2,index,item)" src="/static/image/add.svg"></image>
					</view>
				</view>

				<button class="join-car" @click="joinCart" type="default">加入购物车</button>
			</view>

			<view>
				<u-popup v-model="sucshow" mode="bottom" :closeable='colse'>
					<view class="sucshoe">
						<scroll-view scroll-y="true" style="height: 350rpx;">
							<view v-if="datatype==1">
								<view v-if="dataList.success.length>0" class="sucshoe-txt">
									<view class="suc-txt green">识别成功</view>
									<view class="labes-flex">
										<view class="lf-m-r15" v-for="(item,index) in dataList.success"
											:key="item + index">
											<view class="gren-border green">{{item}}</view>
										</view>
									</view>
								</view>
								<view v-else>暂无数据</view>
							</view>
							<view v-if="datatype==2">
								<view v-if="dataList.fails.length>0" class="sucshoe-txt">
									<view class="suc-txt red">识别失败</view>
									<view class="labes-flex">
										<view class="lf-m-r15" v-for="(item,index) in dataList.fails"
											:key="item + index">
											<view class="red-border red">{{item}}</view>
										</view>
									</view>
								</view>
								<view v-else>暂无数据</view>
							</view>
							<view v-if="datatype==3">
								<view v-if="dataList.vague!=''" class="sucshoe-txt">
									<view class="suc-txt gray">模糊识别</view>
									<view class="content-result" v-for="(item, index) in dataList.vague"
										:key="item + index">
										<span>{{ index }}：</span>
										<view>
											<view v-for="(value, lindex) in item" :key="lindex" class="gray-border"
												:class="{
									                        activebtn: currentSort == index + '/' + lindex
									                      }" @click="tabsecect(value, index, lindex)" :disabled="value.disabled">
												{{ value.name }}
											</view>
										</view>
									</view>
								</view>
								<view v-else>暂无数据</view>
							</view>
						</scroll-view>
					</view>

				</u-popup>
			</view>


		</view>

	</view>
</template>

<script>
	import {
		$https
	} from '@/static/js/request.js';
	import MixinShowprice from '@/static/js/mixinshowprice.js'
	export default {
		data() {
			return {
				//识别出来结果数据
				dataList: {
					list: [],
					vague: {},
					success: [],
					fails: [],
					result: {}
				},
				result: "",
				sucshow: false, //成功打开的弹窗
				colse: true,
				datatype: "", //根据上面选择展示数据
				currentSort: "", //样式切换
				tablist: [], //数据比对
				value: '',
				numshow: false,
				aindex: ""
			}
		},
		methods: {
			//返回上一页面
			back() {
				// #ifdef H5
				uni.hideKeyboard();
				window.history.back(-1);
				// #endif 
				// #ifndef H5
				uni.navigateBack({
					delta: 1
				});
				// #endif	
			},
			//上面弹窗的选择
			succhage(type) {
				this.sucshow = true;
				this.datatype = type
			},
			//模糊识别选择商品的接口	
			tabsecect(value, index, lindex) {
				let params = {
					spu_name_id: value.spu_name_id,
					goods_spu_id: value.id,
					contents: index
				}
				$https('POST', 'vague_search', params, res => {
					if (res.data.errcode == 0) {
						uni.showToast({
							title: res.data.errmsg,
							icon: 'none'
						})
						let goodspu_id = res.data.data.list[0].all.goods_spu_id;
						let arr = this.tablist.map(e => {
							return e.all.goods_spu_id;
						});
						if (!arr.includes(goodspu_id)) {
							this.currentSort = index + "/" + lindex;
							res.data.data.list.map(e => {
								console.log(e)
								e.order_num = parseInt(e.order_num)
							})
							this.tablist.push(res.data.data.list[0]);
							this.sucshow = false
						} else {
							this.currentSort = index + "/" + lindex;
							uni.showToast({
								title: '商品已经存在',
								icon: 'none'
							})
							this.sucshow = false
						}
					}
				})

			},
			//商品数量的添加	
			addgoods(type, index, item) {
				if (type == 2) {
					this.tablist[index].order_num++;
				} else {
					this.tablist[index].order_num--;
					if (this.tablist[index].order_num <= 0) {
						uni.showToast({
							title: '不能在减了',
							icon: 'none'
						})
						this.tablist[index].order_num = 0;
					}
				}
			},
			//批量加入购物车	
			joinCart() {
				let arr = []
				// console.log(this.tablist)
				this.tablist.map(e => {
					arr.push({
						spu_id: e.all.goods_spu_id,
						num: e.order_num,
						goods_id: e.all.goods_id,
					})
				})
				let params = {
					"goods_list": arr
				}
				$https('POST', 'batchAddCart', params, res => {
					console.log(res)
					if (res.data.errcode == 0) {
						uni.showToast({
							title: '添加成功',
							icon: 'none',
							success: function() {
								setTimeout(function() {
									uni.switchTab({
										url: '/pages/tabar/shopcart'
									})
								}, 1000) //延迟时间
							},
						})
					}
				})
			},
			addnum(item) {
				item.showinput = true
			},
			onKeyInput(e, item) {
				e.target.value = (e.target.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null
				//重新赋值给input
				this.$nextTick(() => {
					item.order_num = e.target.value
				})
			},
			onKeyInputBlur(item) {
				item.showinput = false
			}

		},
		onLoad(options) {
			$https('POSTN', 'intelligence_distinguish', {
				contents: options.txt
			}, res => {
				if (res.data.errcode == 0) {
					this.dataList = res.data.data;
					res.data.data.list.map(e => {
						e.order_num = parseInt(e.order_num)
						this.$set(e, 'showinput', false)
					})
					this.tablist = res.data.data.list;
				} else {
					uni.showToast({
						title: res.data.errmsg,
						icon: 'none'
					})
				}
			}, true)
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		height: auto;

		.goods_box {
			height: auto;
			padding: 0rpx 30rpx;
		}

		.identify {
			height: auto;
			padding-top: 40rpx;
		}

		.result-txt {
			color: #181818FF;
			font-size: 30rpx;
			font-weight: bold;
		}

		.list-box {
			display: flex;
			align-items: center;
			padding: 30rpx 0rpx;
		}

		.green-txt {
			padding: 10rpx 30rpx;
			background: $dgreen;
			border-radius: 30px;
			color: white;
		}

		.red-txt {
			padding: 10rpx 30rpx;
			background: #E53535FF;
			border-radius: 30px;
			color: white;
		}

		.gray-txt {
			padding: 10rpx 30rpx;
			border-radius: 30px;
			color: #181818FF;
			background-color: #CCCCCCFF;
		}

		.lf-m-10 {
			margin-right: 30rpx;
		}

		.food-box {
			height: auto;
			overflow-x: auto;
			padding-bottom: 152rpx;

			.shops-box {
				display: flex;
				align-items: center;
				padding-top: 30rpx;
			}

			.pics {
				width: 140rpx;
				height: 140rpx;
			}

			.picture {
				width: 140rpx;
				height: 140rpx;
			}

			.flex1 {
				flex: 1;
			}

			.add-box {
				display: flex;
			}

			.add {
				width: 55rpx;
				height: 55rpx;
			}

			.order{
				margin-top: 3rpx;
			}

			.add-txt {
				padding: 7.5rpx 24rpx;
			}
		}

	}

	.sucshoe {
		background: white;
		height: 400rpx;
		padding: 30rpx;
	}

	.sucshoe-txt {
		height: auto;
	}

	.labes-flex {
		display: flex;
		padding-top: 30rpx;
		flex-wrap: wrap;
	}

	.green {
		color: $dgreen;
		font-size: 30rpx;
	}

	.red {
		color: #E53535FF;
		font-size: 30rpx;
	}

	.gray {
		color: #181818FF;
		font-size: 30rpx;
	}

	.gren-border {
		border: 1px solid $dgreen;
		padding: 8rpx 20rpx;
		border-radius: 26rpx;
	}

	.red-border {
		border: 1px solid #E53535FF;
		padding: 8rpx 20rpx;
		border-radius: 26rpx;
	}

	.gray-border {
		border: 1px solid $dgreen;
		padding: 8rpx 20rpx;
		border-radius: 26rpx;
		color: $dgreen;
	}

	.activebtn {
		background-color: $dgreen;
		color: white !important;
	}

	.content-result {
		display: flex;
		flex-direction: row;
		height: auto;
		color: #8e8e8e;
		align-items: center;
		margin-top: 20rpx;
	}

	.content-result span {
		height: 35px;
		line-height: 35px;
		display: block;
		min-width: 65px;
	}

	.flex1 {
		flex: 1;
	}

	.lf-m-l15 {
		margin-left: 30rpx;
	}

	.name-color {
		font-size: 28rpx;
		font-weight: 500;
		color: #191919;
	}

	.join-car {
		position: fixed;
		bottom: 60rpx;
		right: 0;
		left: 5%;
		width: 90%;
		margin: 0rpx;
		outline: none;
		border-radius: 44rpx;
		background-color: $dgreen;
		color: white;
	}

	button::after {
		border: none;
	}

	.inventory {
		color: #737373;
		font-size: 20rpx;

		>text+text {
			margin-left: 20rpx;
		}
	}

	.lf-m-r15 {
		margin-right: 15rpx;
	}

	/deep/.u-drawer-bottom {
		background-color: white;
	}

	.num-fixed {
		display: flex;
		width: 60%;
		position: fixed;
		left: 20%;
		top: 45%;
		z-index: 10076;
		background-color: white;
		padding: 30rpx;
		border-radius: 10rpx;
		align-items: center;
	}

	.border {
		width: 100rpx;
		margin: 0rpx 10rpx;
		height: 44rpx;
		text-align: center;
		border: 1px solid #cccccc;
		border-radius: 10rpx;
	}
</style>
